package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.annotation.UserParam;
import cn.wolfcode.luowowo.cache.service.ITravelStatisVORedisService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.vo.TravelStatisVO;
import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Travel;
import cn.wolfcode.luowowo.domain.TravelComment;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.query.TravelQuery;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.ITravelCommentService;
import cn.wolfcode.luowowo.service.ITravelService;
import cn.wolfcode.luowowo.util.JsonResult;
import cn.wolfcode.luowowo.util.UMEditorUploader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("travel")
public class TravelController {
    @Autowired
    private ITravelService travelService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private ITravelCommentService travelCommentService;

    @Autowired
    private ITravelStatisVORedisService travelStatisVORedisService;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @GetMapping("/query")
    public Object query(@ModelAttribute("qo")TravelQuery qo) {
        Page<Travel> page = travelService.queryOrderByViewnum(qo);

        List<Travel> travels = page.getContent();
        Travel travel = travels.get(0);
        System.out.println(travel);

        return JsonResult.success(page);
    }

    @RequireLogin
    @GetMapping("/input")
    public Object input(String id) {
        Map<String, Object> map = new HashMap<>();
        List<Destination> destinations = destinationService.listAll();
        map.put("dests", destinations);
        if (StringUtils.hasLength(id)) {
            Travel travel = travelService.select(id);
            map.put("tv", travel);
            return  JsonResult.success(map);
        } else {
            return  JsonResult.success(map);
        }
    }

    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(Travel travel, HttpServletRequest request) {
        String token = request.getHeader("token");
        String id;
        if (StringUtils.hasLength(travel.getId())) {
            travelService.update(travel);
            id = travel.getId();
        } else {
            id = travelService.insert(travel, token);
        }
        return JsonResult.success(id);
    }

    @GetMapping("detail")
    public Object detail(String id) {
        Travel travel = travelService.select(id);
        travelStatisVORedisService.viewnumIncr(id);
        return JsonResult.success(travel);
    }

    @GetMapping("/comments")
    public Object comments(String travelId) {
        List<TravelComment> travelComments = travelCommentService.listByTravelId(travelId);
        return JsonResult.success(travelComments);
    }

    @RequireLogin
    @PostMapping("/commentAdd")
    public Object commentAdd(TravelComment travelComment, HttpServletRequest request) {
        String token = request.getHeader("token");
        travelCommentService.insert(travelComment, token);
        return JsonResult.success();
    }

    @GetMapping("/newComments")
    public Object newComments(@ModelAttribute("qo") TravelQuery qo) {
        Page<Travel> page = travelService.queryOrderByViewnum(qo);
        List<Travel> travels = page.getContent();
        List<TravelComment> comments = travelCommentService.listByTravelId(travels.get(0).getId());
        return JsonResult.success(comments);
    }

    @GetMapping("/statisVO")
    public Object statisVO(String tid) {
        TravelStatisVO vo = travelStatisVORedisService.get(tid);
        return JsonResult.success(vo);
    }

    @RequireLogin
    @GetMapping("/favor")
    public Object favor(String tid, @UserParam UserInfo userInfo) {
        boolean flag = userInfoRedisService.tidFavor(tid, userInfo.getId());
        if (flag) {
            return JsonResult.success(false);
        } else {
            return JsonResult.success(true);
        }
    }

    @RequireLogin
    @GetMapping("/thumbup")
    public Object thumbup(String tid, @UserParam UserInfo userInfo) {
        boolean flag = travelStatisVORedisService.thumbup(tid, userInfo.getId());
        if (flag) {
            return JsonResult.success(false);
        } else {
            return JsonResult.success(true);
        }
    }

    //上传图片
    @RequestMapping("/contentImage")
    @ResponseBody
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }
}
